package com.lq.demo.demo.mode.limit;

import com.google.common.util.concurrent.RateLimiter;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;

import javax.servlet.ServletContext;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 定时扫描servletContext里的limitcount 可以对其重置 修改等
 */
@Configuration
@EnableScheduling
public class TimerService  implements  ServletContextAware{

    private ServletContext servletContext;


    //这里可以设置每日0:00 点进行次数重置  下面这只是演示
    @Scheduled(initialDelay = 1000,fixedRate = 2000)
    public void timerToNow() {
        int count = (int) servletContext.getAttribute("limit");
        servletContext.setAttribute("limit","自定义总次数");
        System.out.println("  count limit:" + count);
        System.out.println("now time:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    }

    @Override
    public void setServletContext(ServletContext servletContext) {
        this.servletContext=servletContext;
    }
}
